//
//  TCScrollTextView.m
//  Game1537
//
//  Created by apple on 2020/10/19.
//

#import "TCScrollTextView.h"

@interface TCScrollTextView ()

@property (nonatomic, strong) UILabel * contentLabel;
@end
@implementation TCScrollTextView

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder{
    if (self == [super initWithCoder:coder]) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:_contentLabel];
    
    //超出部分不显示
    self.layer.masksToBounds = YES;
    //设置默认值
    self.speed = 0.5;
    //解决进入后台然后回来就停了
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startAnimation) name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void)startAnimation {
    [self setNeedsLayout];
}

- (void)setText:(NSString *)text {
    _text = text;
    _contentLabel.text = text;
    //根据内容得到_contentLabel的frame
    [_contentLabel sizeToFit];
    //居中显示
    CGRect frame = _contentLabel.frame;
    frame.origin.y = (self.bounds.size.height - frame.size.height)/2;
    _contentLabel.frame = frame;

}
- (void)setTextArr:(NSArray *)textArr{
    _textArr = textArr;
    // 移除所有button
    for(UIView *view in [self subviews]) {
        if ([view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    CGFloat startX = 0.00;
    for (int i = 0; i < _textArr.count; i++) {
        NSString *str = [NSString stringWithFormat:@"%@",_textArr[i]];
        CGFloat witdh = [self widthOfString:str andFont:[UIFont systemFontOfSize:15] andHeight:18];
        startX += witdh;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(startX-witdh, 0, witdh, 25);
        button.backgroundColor = [UIColor clearColor];
        button.tag = i;
        [button addTarget:self action:@selector(textAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
}

- (void)setFont:(UIFont *)font {
    _font = font;
    _contentLabel.font = font;
}

- (void)setColor:(UIColor *)color{
    _color = color;
    _contentLabel.textColor = _color;
}

- (void)setSpeed:(CGFloat)speed {
    _speed = speed;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat sizeWidth = CGRectGetWidth(_contentLabel.frame);
    
    [self.contentLabel.layer removeAnimationForKey:@"keyFrame"];
    //如果文字能够显示完全 则不用滚动显示
    if (sizeWidth<= self.bounds.size.width) {
        return;
    }
    
    //添加帧动画 实现滚动效果 其实就是改变x的值
    CAKeyframeAnimation* keyFrame = [CAKeyframeAnimation animation];
    keyFrame.keyPath = @"transform.translation.x";
    keyFrame.values = @[@(0), @(-sizeWidth + self.bounds.size.width)];
    keyFrame.repeatCount = NSIntegerMax;
    keyFrame.autoreverses = NO;
    keyFrame.duration =  self.speed * self.contentLabel.text.length*0.8;
    //滚动速度改变 可以实现滚动速度由慢变快或者由快变慢
//    keyFrame.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], [CAMediaTimingFunction functionWithControlPoints:0 :0 :0.5 :0.5]];
    [self.contentLabel.layer addAnimation:keyFrame forKey:@"keyFrame"];

}
- (void)textAction:(UIButton *)sender{
    if (self.block) {
        self.block(@(sender.tag));
    }
}
- (CGFloat)widthOfString:(NSString *)str andFont:(UIFont *)font andHeight:(CGFloat)height{
    return [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil].size.width;
}
@end
